package cn.edu.sctu.sctu_lysd.view.act;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.text.ParseException;

import cn.edu.sctu.sctu_lysd.R;
import cn.edu.sctu.sctu_lysd.alipay.PayActivity;
import cn.edu.sctu.sctu_lysd.base.BaseActivity;
import cn.edu.sctu.sctu_lysd.bean.ParcelData;
import cn.edu.sctu.sctu_lysd.biz.OrderDetailedMessageBiz;
import cn.edu.sctu.sctu_lysd.cofig.CacheKeyConfig;
import cn.edu.sctu.sctu_lysd.cofig.Status;
import cn.edu.sctu.sctu_lysd.databinding.ActivityMyOrdersDetailedBinding;
import cn.edu.sctu.sctu_lysd.my_view.CustomDialog;
import cn.edu.sctu.sctu_lysd.presenter.OrderDetailedPresenter;
import cn.edu.sctu.sctu_lysd.utils.DateUtils;
import cn.edu.sctu.sctu_lysd.utils.JumpUtils;
import cn.edu.sctu.sctu_lysd.utils.L;
import cn.edu.sctu.sctu_lysd.utils.ProgressUtil;
import cn.edu.sctu.sctu_lysd.utils.SharePreferenceUtils;
import io.rong.imkit.RongIM;

/**
 * Created by wangfuxin on 2017-04-14.
 */

public class OrderDetailedAct extends BaseActivity implements OrderDetailedMessageBiz.View {

    public final static String INTENT_ID_TAG = "parcelId";
    public final static String INTENT_IS_MY_ORDER_TAG = "order_type";

    private OrderDetailedMessageBiz.Presenter mPresenter;

    private ActivityMyOrdersDetailedBinding mBinding;

    public final static int MY_ORDER = 0;
    public final static int NOT_MY_ORDER = 1;

    private int orderType;

    private long parcelId = 0;

    private long creatorId = 0;

    private ProgressUtil progressUtil;
    private ProgressDialog progressDialog;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_my_orders_detailed;
    }

    @Override
    protected void initAfter(Object bind) {

        mBinding = (ActivityMyOrdersDetailedBinding) bind;

        progressUtil = ProgressUtil.getInstance(this);

        initPresenter(new OrderDetailedPresenter(this, this));

        getIntentData();

        mPresenter.getOrder(parcelId);

    }

    private void getIntentData() {

        Bundle bundle = getIntent().getExtras();

        parcelId = bundle.getLong(INTENT_ID_TAG);

        orderType = bundle.getInt(INTENT_IS_MY_ORDER_TAG);

    }

    public void parcelDetailedBack(View view) {

        finish();

    }

    @Override
    public void initPresenter(OrderDetailedMessageBiz.Presenter presenter) {

        this.mPresenter = presenter;

        mPresenter.start();

    }

    @Override
    public void getOrderSuccess(final ParcelData data) {

        creatorId = data.getCreator();//初始化创建者ID  用于接受成功后由系统发送消息提醒他

        try {

            if (orderType == MY_ORDER) {
                L.e("sssss", data.getOrderStatus() + "");
                switch (data.getOrderStatus()) {

                    case Status.WAIT_BUYER_PAY:

                        mBinding.acceptBtn.setText("点击支付");

                        mBinding.acceptBtn.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                                Bundle bundle = new Bundle();

                                bundle.putLong(PayActivity.ORDER_ID_INTENT_TAG, data.getOrderId());

                                JumpUtils.jumpActivity(OrderDetailedAct.this, PayActivity.class, bundle);

                            }
                        });

                        break;
                    case Status.TRADE_SUCCESS://支付成功

                        L.e("getPostman", data.getPostman() + "ssss");

                        if (data.getPostman() == 0) {
                            mBinding.acceptBtn.setText("您的包裹还没有找到送货者");
                            mBinding.acceptBtn.setBackgroundColor(Color.parseColor("#FF434242"));

                            mBinding.acceptBtn.setClickable(false);

                        } else {
                            mBinding.acceptBtn.setText("联系接收者");

                            mBinding.acceptBtn.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    //启动会话界面
                                    if (RongIM.getInstance() != null)
                                        RongIM.getInstance().startPrivateChat(OrderDetailedAct.this,
                                                String.valueOf(data.getPostman()),
                                                SharePreferenceUtils.getInstance(OrderDetailedAct.this).getString(CacheKeyConfig.CACHE_USER_NICKNAME));
                                }
                            });

                        }

                        break;

                }
            }

            if (orderType == NOT_MY_ORDER) {

                mBinding.acceptBtn.setText("确认接单");

                mBinding.acceptBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        progressDialog = progressUtil.getProgressDialog(ProgressDialog.STYLE_HORIZONTAL, true, "");

                        progressDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                            @Override
                            public void onDismiss(DialogInterface dialog) {

                                mPresenter.acceptCallCancel();

                            }
                        });

                        mPresenter.accept(data.getId());
                    }
                });

            }

            switch (data.getIsSuccess()) {

                case Status.PARCEL_IS_NOT_SUCCESS://包裹未送达

                    mBinding.parcelStatus.setText("包裹未送达");

                    break;
                case Status.PARCEL_IS_SUCCESS://

                    mBinding.parcelStatus.setText("包裹已送达");

                case Status.PARCEL_IS_SUCCESS_://

                    mBinding.parcelStatus.setText("包裹已送达");

                    break;

            }

            mBinding.startPlaceTxt.setText(data.getOrigin());
            mBinding.endPlaceTxt.setText(data.getDestination());

            mBinding.startTimeTxt.setText(DateUtils.dateToStr(data.getStartTime(), DateUtils.DATE_SDF));
            mBinding.endTimeTxt.setText(DateUtils.dateToStr(data.getEndTime(), DateUtils.DATE_SDF));

            mBinding.nameAndPhoneTxt.setText(data.getReceiverName() + " : " + data.getReceiverPhone());
            mBinding.addresseePlaceTxt.setText(data.getDestination());

            mBinding.goodsNameTxt.setText(data.getName());
            mBinding.goodsWeightTxt.setText(data.getWeight() + "kg");
            mBinding.goodsDescriptionTxt.setText(data.getDescription());

            mBinding.price.setText(data.getOrderAmount() + "元");

            mBinding.goodsImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ShowImgAct.intentThis(OrderDetailedAct.this, data.getParcelPhotoUrl());
                }
            });

            Picasso.with(this).load(data.getParcelPhotoUrl()).into(mBinding.goodsImg);


        } catch (ParseException e) {
            e.printStackTrace();
        }


    }

    @Override
    public void getOrderFailed() {

    }


    @Override
    public void acceptSuccess() {

        progressUtil.dismiss(progressDialog);

        ProgressUtil.showAlertDialog("", "接单成功，立即去和包裹主人聊聊", this, new ProgressUtil.AlertDialogCallBack() {
            @Override
            public void submit() {

                contactCreator(creatorId);

                finish();

            }

            @Override
            public void cancel() {

            }
        });


    }

    private void contactCreator(long creatorId) {
        //启动会话界面
        if (RongIM.getInstance() != null)
            RongIM.getInstance().startPrivateChat(OrderDetailedAct.this,
                    String.valueOf(creatorId),
                    "title");
    }

    @Override
    public void acceptFailed() {

        progressUtil.dismiss(progressDialog);

        Toast.makeText(this, "接受失败", Toast.LENGTH_SHORT).show();

    }

    /**
     * 未缴纳 押金
     */
    @Override
    public void notDeposit() {

        showAlertDialog();

    }

    public void showAlertDialog() {

        CustomDialog.Builder builder = new CustomDialog.Builder(this);
        builder.setMessage("点击确定交取押金");
        builder.setTitle("老哥");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

                progressUtil.dismiss(progressDialog);

                JumpUtils.jumpActivity(OrderDetailedAct.this,DepositAct.class);

                dialog.dismiss();

            }
        });

        builder.setNegativeButton("取消",
                new android.content.DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        builder.create().show();
    }
}
